/*****************************************************************//**
 * \file   SurgicalRobotConfig.cpp
 * \brief  这个文件处理微创手术机器人实验平台的各个机器人的参数配置
 *         主要包括。机械臂的类型，机械臂末端器械的配置，坐标系选择，
 *         登录机械臂的网络等相关配置
 * 
 * \author Administrator
 * \date   July 2021
 * \modify by Administrator
 * \date   July 2021

 *********************************************************************/
#include "./include/SurgicalRobotConfig.h"
#include "./include/SurgicalRobotCommon.h"

/**
 * \brief   SurgicalRobotConfig类的构造函数.
 * 
 * \param parent  父窗体，默认为null
 */
SurgicalRobotConfig::SurgicalRobotConfig(QWidget* parent)
    : QWidget(parent)
{
    ui.setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);//自动析构
    this->setWindowTitle(QStringLiteral("配置机械臂"));
    this->setWindowIcon(QIcon("hfUOT.png"));

    SRCommunicat->InitConfigParam();

    /*********************************robot1*********************************/
    //初始化控件
    QRegExp rx("((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)");
    QValidator* validator_11 = new QRegExpValidator(rx, ui.Robot1IP);//设置输入值范围
    ui.Robot1IP->setValidator(validator_11);
    ui.Robot1IP->setText(sRobotConfigPara[0].cRobotArmIP);
    QValidator* validator_12 = new QRegExpValidator(rx, ui.Robot1MA);
    ui.Robot1MA->setValidator(validator_12);
    ui.Robot1MA->setText(sRobotConfigPara[0].cRobotArmMA);
    QValidator* validator_13 = new QRegExpValidator(rx, ui.Robot1GW);
    ui.Robot1GW->setValidator(validator_13);
    ui.Robot1GW->setText(sRobotConfigPara[0].cRobotArmGW);
    ui.Robot1Choice->insertItem(0, "NULL", 0);
    ui.Robot1Choice->insertItem(1, "AOBOi5", 1);
    ui.Robot1Choice->insertItem(2, "UR3", 2);
    ui.Robot1Choice->insertItem(3, "FRANKA", 3);
    ui.Robot1Choice->insertItem(4, "Others", 4);
    ui.Robot1Choice->setCurrentIndex(sRobotConfigPara[0].iRobotarmType);
    ui.Robot1EndToolChoice->insertItem(0, "NULL", 0);
    ui.Robot1EndToolChoice->insertItem(1, "CAMERA", 1);
    ui.Robot1EndToolChoice->insertItem(2, "BoplarHook", 2);
    ui.Robot1EndToolChoice->insertItem(3, "4DOF", 3);
    ui.Robot1EndToolChoice->insertItem(4, "TRANDITION", 4);
    ui.Robot1EndToolChoice->insertItem(5, "FORCEP", 5);
    ui.Robot1EndToolChoice->insertItem(6, "Others", 6);
    ui.Robot1EndToolChoice->setCurrentIndex(sRobotConfigPara[0].iRobotToolType);

    ui.Robot1Motor1->setValue(sRobotConfigPara[0].iMotornumber[0]);
    ui.Robot1Motor2->setValue(sRobotConfigPara[0].iMotornumber[1]);
    ui.Robot1Motor3->setValue(sRobotConfigPara[0].iMotornumber[2]);
    ui.Robot1Motor4->setValue(sRobotConfigPara[0].iMotornumber[3]);
    ui.Robot1Motor5->setValue(sRobotConfigPara[0].iMotornumber[4]);

    ui.Robot1Axis->setText(sRobotConfigPara[0].cCoordinateSystem);
    //按键信号槽绑定
    connect(ui.btRobot1Save, SIGNAL(clicked()), this, SLOT(Robot1Save()));
    connect(ui.btRobot1Reset, SIGNAL(clicked()), this, SLOT(Robot1Reset()));

    /*********************************robot2*********************************/
    //初始化控件
    validator_11 = new QRegExpValidator(rx, ui.Robot2IP);//设置输入值范围
    ui.Robot2IP->setValidator(validator_11);
    ui.Robot2IP->setText(sRobotConfigPara[1].cRobotArmIP);
    validator_12 = new QRegExpValidator(rx, ui.Robot2MA);
    ui.Robot2MA->setValidator(validator_12);
    ui.Robot2MA->setText(sRobotConfigPara[1].cRobotArmMA);
    validator_13 = new QRegExpValidator(rx, ui.Robot2GW);
    ui.Robot2GW->setValidator(validator_13);
    ui.Robot2GW->setText(sRobotConfigPara[1].cRobotArmGW);
    ui.Robot2Choice->insertItem(0, "NULL", 0);
    ui.Robot2Choice->insertItem(1, "AOBOi5", 1);
    ui.Robot2Choice->insertItem(2, "UR3", 2);
    ui.Robot2Choice->insertItem(3, "FRANKA", 3);
    ui.Robot2Choice->insertItem(4, "Others", 4);
    ui.Robot2Choice->setCurrentIndex(sRobotConfigPara[1].iRobotarmType);
    ui.Robot2EndToolChoice->insertItem(0, "NULL", 0);
    ui.Robot2EndToolChoice->insertItem(1, "CAMERA", 1);
    ui.Robot2EndToolChoice->insertItem(2, "BoplarHook", 2);
    ui.Robot2EndToolChoice->insertItem(3, "4DOF", 3);
    ui.Robot2EndToolChoice->insertItem(4, "TRANDITION", 4);
	ui.Robot2EndToolChoice->insertItem(5, "FORCEP", 5);
	ui.Robot2EndToolChoice->insertItem(6, "Others", 6);
    ui.Robot2EndToolChoice->setCurrentIndex(sRobotConfigPara[1].iRobotToolType);
    ui.Robot2Axis->setText(sRobotConfigPara[1].cCoordinateSystem);
    ui.Robot2Motor1->setValue(sRobotConfigPara[1].iMotornumber[0]);
    ui.Robot2Motor2->setValue(sRobotConfigPara[1].iMotornumber[1]);
    ui.Robot2Motor3->setValue(sRobotConfigPara[1].iMotornumber[2]);
    ui.Robot2Motor4->setValue(sRobotConfigPara[1].iMotornumber[3]);
    ui.Robot2Motor5->setValue(sRobotConfigPara[1].iMotornumber[4]);

    //按键信号槽绑定
    connect(ui.btRobot2Save, SIGNAL(clicked()), this, SLOT(Robot2Save()));
    connect(ui.btRobot2Reset, SIGNAL(clicked()), this, SLOT(Robot2Reset()));

    /*********************************robot3*********************************/
    //初始化控件
    validator_11 = new QRegExpValidator(rx, ui.Robot3IP);////设置输入值范围
    ui.Robot3IP->setValidator(validator_11);
    ui.Robot3IP->setText(sRobotConfigPara[2].cRobotArmIP);
    validator_12 = new QRegExpValidator(rx, ui.Robot3MA);
    ui.Robot3MA->setValidator(validator_12);
    ui.Robot3MA->setText(sRobotConfigPara[2].cRobotArmMA);
    validator_13 = new QRegExpValidator(rx, ui.Robot3GW);
    ui.Robot3GW->setValidator(validator_13);
    ui.Robot3GW->setText(sRobotConfigPara[2].cRobotArmGW);
    ui.Robot3Choice->insertItem(0, "NULL", 0);
    ui.Robot3Choice->insertItem(1, "AOBO5", 1);
    ui.Robot3Choice->insertItem(2, "UR3", 2);
    ui.Robot3Choice->insertItem(3, "FRANKA", 3);
    ui.Robot3Choice->insertItem(4, "Others", 4);
    ui.Robot3Choice->setCurrentIndex(sRobotConfigPara[2].iRobotarmType);
    ui.Robot3EndToolChoice->insertItem(0, "NULL", 0);
    ui.Robot3EndToolChoice->insertItem(1, "CAMERA", 1);
    ui.Robot3EndToolChoice->insertItem(2, "BoplarHook", 2);
    ui.Robot3EndToolChoice->insertItem(3, "4DOF", 3);
    ui.Robot3EndToolChoice->insertItem(4, "TRANDITION", 4);
	ui.Robot3EndToolChoice->insertItem(5, "FORCEP", 5);
	ui.Robot3EndToolChoice->insertItem(6, "Others", 6);
    ui.Robot3EndToolChoice->setCurrentIndex(sRobotConfigPara[2].iRobotToolType);

    ui.Robot3Motor1->setValue(sRobotConfigPara[2].iMotornumber[0]);
    ui.Robot3Motor2->setValue(sRobotConfigPara[2].iMotornumber[1]);
    ui.Robot3Motor3->setValue(sRobotConfigPara[2].iMotornumber[2]);
    ui.Robot3Motor4->setValue(sRobotConfigPara[2].iMotornumber[3]);
    ui.Robot3Motor5->setValue(sRobotConfigPara[2].iMotornumber[4]);
    ui.Robot3Axis->setText(sRobotConfigPara[2].cCoordinateSystem);
    //按键信号槽绑定
    connect(ui.btRobot3Save, SIGNAL(clicked()), this, SLOT(Robot3Save()));
    connect(ui.btRobot3Reset, SIGNAL(clicked()), this, SLOT(Robot3Reset()));

    /*********************************robot4*********************************/
   //初始化控件
    validator_11 = new QRegExpValidator(rx, ui.Robot4IP);////设置输入值范围
    ui.Robot4IP->setValidator(validator_11);
    ui.Robot4IP->setText(sRobotConfigPara[3].cRobotArmIP);
    validator_12 = new QRegExpValidator(rx, ui.Robot4MA);
    ui.Robot4MA->setValidator(validator_12);
    ui.Robot4MA->setText(sRobotConfigPara[3].cRobotArmMA);
    validator_13 = new QRegExpValidator(rx, ui.Robot4GW);
    ui.Robot4GW->setValidator(validator_13);
    ui.Robot4GW->setText(sRobotConfigPara[3].cRobotArmGW);
    ui.Robot4Choice->insertItem(0, "NULL", 0);
    ui.Robot4Choice->insertItem(1, "AOBO5", 1);
    ui.Robot4Choice->insertItem(2, "UR3", 2);
    ui.Robot4Choice->insertItem(3, "FRANKA", 3);
    ui.Robot4Choice->insertItem(4, "Others", 4);
    ui.Robot4Choice->setCurrentIndex(sRobotConfigPara[3].iRobotarmType);
    ui.Robot4EndToolChoice->insertItem(0, "NULL", 0);
    ui.Robot4EndToolChoice->insertItem(1, "CAMERA", 1);
    ui.Robot4EndToolChoice->insertItem(2, "BoplarHook", 2);
    ui.Robot4EndToolChoice->insertItem(3, "4DOF", 3);
    ui.Robot4EndToolChoice->insertItem(4, "TRANDITION", 4);
	ui.Robot4EndToolChoice->insertItem(5, "FORCEP", 5);
	ui.Robot4EndToolChoice->insertItem(6, "Others", 6);
    ui.Robot4EndToolChoice->setCurrentIndex(sRobotConfigPara[3].iRobotToolType);
    ui.Robot4Motor1->setValue(sRobotConfigPara[3].iMotornumber[0]);
    ui.Robot4Motor2->setValue(sRobotConfigPara[3].iMotornumber[1]);
    ui.Robot4Motor3->setValue(sRobotConfigPara[3].iMotornumber[2]);
    ui.Robot4Motor4->setValue(sRobotConfigPara[3].iMotornumber[3]);
    ui.Robot4Motor5->setValue(sRobotConfigPara[3].iMotornumber[4]);
    ui.Robot4Axis->setText(sRobotConfigPara[3].cCoordinateSystem);
    //按键信号槽绑定
    connect(ui.btRobot4Save, SIGNAL(clicked()), this, SLOT(Robot4Save()));
    connect(ui.btRobot4Reset, SIGNAL(clicked()), this, SLOT(Robot4Reset()));
}

/**
 * \brief   SurgicalRobotConfig类的析构函数.
 * 
 */
SurgicalRobotConfig::~SurgicalRobotConfig()
{

}


/**
 * \brief  robot1配置参数重置.
 * 
 */
void SurgicalRobotConfig::Robot1Reset()
{
    strcpy(sRobotConfigPara[0].cRobotArmIP, "192.168.65.140");
    strcpy(sRobotConfigPara[0].cRobotArmGW, "255.255.255.0");
    strcpy(sRobotConfigPara[0].cRobotArmMA, "192.168.65.1");
    //机械臂选择
    sRobotConfigPara[0].iRobotarmType = ARMTYPE_AOBOi5;
    //器械选择
    sRobotConfigPara[0].iRobotToolType = TOOLTYPE_BoplarHook;
    //坐标系设置
    strcpy(sRobotConfigPara[0].cCoordinateSystem, "X,Y,Z");
    //控件（ip地址等）重置
    ui.Robot1IP->setText(sRobotConfigPara[0].cRobotArmIP);
    ui.Robot1MA->setText(sRobotConfigPara[0].cRobotArmMA);
    ui.Robot1GW->setText(sRobotConfigPara[0].cRobotArmGW);
    ui.Robot1Choice->setCurrentIndex(sRobotConfigPara[0].iRobotarmType);
    ui.Robot1EndToolChoice->setCurrentIndex(sRobotConfigPara[0].iRobotToolType);
    ui.Robot1Axis->setText(sRobotConfigPara[0].cCoordinateSystem);

    ui.Robot1Motor1->setValue(0);
    ui.Robot1Motor2->setValue(1);
    ui.Robot1Motor3->setValue(2);
    ui.Robot1Motor4->setValue(3);
    ui.Robot1Motor5->setValue(4);

    return;
}

/**
 * \brief   保存robot1的配置参数.
 * 
 */
void SurgicalRobotConfig::Robot1Save()
{
    strcpy(sRobotConfigPara[0].cRobotArmIP, ui.Robot1IP->text().toStdString().c_str());
    strcpy(sRobotConfigPara[0].cRobotArmMA, ui.Robot1MA->text().toStdString().c_str());
    strcpy(sRobotConfigPara[0].cRobotArmGW, ui.Robot1GW->text().toStdString().c_str());
    sRobotConfigPara[0].iRobotarmType = ui.Robot1Choice->currentIndex();//当前数据currentData() 当前索引currentIndex()
    sRobotConfigPara[0].iRobotToolType = ui.Robot1EndToolChoice->currentIndex();
    strcpy(sRobotConfigPara[0].cCoordinateSystem, ui.Robot1Axis->text().toStdString().c_str());
    sRobotConfigPara[0].iMotornumber[0] = ui.Robot1Motor1->value();
    sRobotConfigPara[0].iMotornumber[1] = ui.Robot1Motor2->value();
    sRobotConfigPara[0].iMotornumber[2] = ui.Robot1Motor3->value();
    sRobotConfigPara[0].iMotornumber[3] = ui.Robot1Motor4->value();
    sRobotConfigPara[0].iMotornumber[4] = ui.Robot1Motor5->value();

    bool res = SRCommunicat->SaveRobotConfig();
    if(res) emit show_message(LOG_MES::logMessages["Info1"]);
    else emit show_message(LOG_MES::logMessages["Info2"]);

    return;
}

/**
 * \brief   robot2配置参数重置.
 * 
 */
void SurgicalRobotConfig::Robot2Reset()
{
    strcpy(sRobotConfigPara[1].cRobotArmIP, "192.168.65.138");
    strcpy(sRobotConfigPara[1].cRobotArmGW, "255.255.255.0");
    strcpy(sRobotConfigPara[1].cRobotArmMA, "192.168.65.1");
    //机械臂选择
    sRobotConfigPara[1].iRobotarmType = ARMTYPE_AOBOi5;
    //器械选择
    sRobotConfigPara[1].iRobotToolType = TOOLTYPE_4DOF;
    //坐标系设置
    strcpy(sRobotConfigPara[1].cCoordinateSystem, "X,Y,Z");
    //控件（ip地址等）重置
    ui.Robot2IP->setText(sRobotConfigPara[1].cRobotArmIP);
    ui.Robot2MA->setText(sRobotConfigPara[1].cRobotArmMA);
    ui.Robot2GW->setText(sRobotConfigPara[1].cRobotArmGW);
    ui.Robot2Choice->setCurrentIndex(sRobotConfigPara[1].iRobotarmType);
    ui.Robot2EndToolChoice->setCurrentIndex(sRobotConfigPara[1].iRobotToolType);
    ui.Robot2Axis->setText(sRobotConfigPara[1].cCoordinateSystem);


    ui.Robot2Motor1->setValue(5);
    ui.Robot2Motor2->setValue(6);
    ui.Robot2Motor3->setValue(7);
    ui.Robot2Motor4->setValue(8);
    ui.Robot2Motor5->setValue(9);

    return;
}

/**
 * \brief   robot2配置参数保存.
 * 
 */
void SurgicalRobotConfig::Robot2Save()
{
    strcpy(sRobotConfigPara[1].cRobotArmIP, ui.Robot2IP->text().toStdString().c_str());
    strcpy(sRobotConfigPara[1].cRobotArmMA, ui.Robot2MA->text().toStdString().c_str());
    strcpy(sRobotConfigPara[1].cRobotArmGW, ui.Robot2GW->text().toStdString().c_str());
    sRobotConfigPara[1].iRobotarmType = ui.Robot2Choice->currentIndex();//当前数据currentData() 当前索引currentIndex()
    sRobotConfigPara[1].iRobotToolType = ui.Robot2EndToolChoice->currentIndex();
    strcpy(sRobotConfigPara[1].cCoordinateSystem, ui.Robot2Axis->text().toStdString().c_str());
    sRobotConfigPara[1].iMotornumber[0] = ui.Robot2Motor1->value();
    sRobotConfigPara[1].iMotornumber[1] = ui.Robot2Motor2->value();
    sRobotConfigPara[1].iMotornumber[2] = ui.Robot2Motor3->value();
    sRobotConfigPara[1].iMotornumber[3] = ui.Robot2Motor4->value();
    sRobotConfigPara[1].iMotornumber[4] = ui.Robot2Motor5->value();
    
    bool res = SRCommunicat->SaveRobotConfig();
    if(res) emit show_message(LOG_MES::logMessages["Info1"]);
    else emit show_message(LOG_MES::logMessages["Info2"]);
    return;
}

/**
 * \brief  robot3 配置参数重置.
 * 
 */
void SurgicalRobotConfig::Robot3Reset()
{
    strcpy(sRobotConfigPara[2].cRobotArmIP, "192.168.65.139");
    strcpy(sRobotConfigPara[2].cRobotArmGW, "255.255.255.0");
    strcpy(sRobotConfigPara[2].cRobotArmMA, "192.168.65.1");
    //机械臂选择
    sRobotConfigPara[2].iRobotarmType = ARMTYPE_AOBOi5;
    //器械选择
    sRobotConfigPara[2].iRobotToolType = TOOLTYPE_BoplarHook;
    //坐标系设置
    strcpy(sRobotConfigPara[2].cCoordinateSystem, "X,Y,Z");
    //控件（ip地址等）重置
    ui.Robot3IP->setText(sRobotConfigPara[2].cRobotArmIP);
    ui.Robot3MA->setText(sRobotConfigPara[2].cRobotArmMA);
    ui.Robot3GW->setText(sRobotConfigPara[2].cRobotArmGW);
    ui.Robot3Choice->setCurrentIndex(sRobotConfigPara[2].iRobotarmType);
    ui.Robot3EndToolChoice->setCurrentIndex(sRobotConfigPara[2].iRobotToolType);
    ui.Robot3Axis->setText(sRobotConfigPara[2].cCoordinateSystem);

    ui.Robot3Motor1->setValue(10);
    ui.Robot3Motor2->setValue(11);
    ui.Robot3Motor3->setValue(12);
    ui.Robot3Motor4->setValue(13);
    ui.Robot3Motor5->setValue(14);
    
    return;
}

/**
 * \brief  robot3 配置参数保存 .
 * 
 */
void SurgicalRobotConfig::Robot3Save()
{
    strcpy(sRobotConfigPara[2].cRobotArmIP, ui.Robot3IP->text().toStdString().c_str());
    strcpy(sRobotConfigPara[2].cRobotArmMA, ui.Robot3MA->text().toStdString().c_str());
    strcpy(sRobotConfigPara[2].cRobotArmGW, ui.Robot3GW->text().toStdString().c_str());
    sRobotConfigPara[2].iRobotarmType = ui.Robot3Choice->currentIndex();//当前数据currentData() 当前索引currentIndex()
    sRobotConfigPara[2].iRobotToolType = ui.Robot3EndToolChoice->currentIndex();
    strcpy(sRobotConfigPara[2].cCoordinateSystem, ui.Robot3Axis->text().toStdString().c_str());
    sRobotConfigPara[2].iMotornumber[0] = ui.Robot3Motor1->value();
    sRobotConfigPara[2].iMotornumber[1] = ui.Robot3Motor2->value();
    sRobotConfigPara[2].iMotornumber[2] = ui.Robot3Motor3->value();
    sRobotConfigPara[2].iMotornumber[3] = ui.Robot3Motor4->value();
    sRobotConfigPara[2].iMotornumber[4] = ui.Robot3Motor5->value();
    
    bool res = SRCommunicat->SaveRobotConfig();
    if(res) emit show_message(LOG_MES::logMessages["Info1"]);
    else emit show_message(LOG_MES::logMessages["Info2"]);
    return;
}

/**
 * \brief   robot4配置参数重置.
 * 
 */
void SurgicalRobotConfig::Robot4Reset()
{
    strcpy(sRobotConfigPara[3].cRobotArmIP, "192.168.65.110");
    strcpy(sRobotConfigPara[3].cRobotArmGW, "255.255.255.0");
    strcpy(sRobotConfigPara[3].cRobotArmMA, "192.168.65.1");
    //机械臂选择
    sRobotConfigPara[3].iRobotarmType = ARMTYPE_FRANKA;
    //器械选择
    sRobotConfigPara[3].iRobotToolType = TOOLTYPE_CAMERA;
    //坐标系选择
    strcpy(sRobotConfigPara[3].cCoordinateSystem, "X,Y,Z");
    //控件（ip地址等）重置
    ui.Robot4IP->setText(sRobotConfigPara[3].cRobotArmIP);
    ui.Robot4MA->setText(sRobotConfigPara[3].cRobotArmMA);
    ui.Robot4GW->setText(sRobotConfigPara[3].cRobotArmGW);
    ui.Robot4Choice->setCurrentIndex(sRobotConfigPara[3].iRobotarmType);
    ui.Robot4EndToolChoice->setCurrentIndex(sRobotConfigPara[3].iRobotToolType);
    ui.Robot4Axis->setText(sRobotConfigPara[3].cCoordinateSystem);

    ui.Robot4Motor1->setValue(15);
    ui.Robot4Motor2->setValue(16);
    ui.Robot4Motor3->setValue(17);
    ui.Robot4Motor4->setValue(18);
    ui.Robot4Motor5->setValue(19);

    return;
}

/**
 * \brief   robot4配置参数保存.
 * 
 */
void SurgicalRobotConfig::Robot4Save()
{
    strcpy(sRobotConfigPara[3].cRobotArmIP, ui.Robot4IP->text().toStdString().c_str());
    strcpy(sRobotConfigPara[3].cRobotArmMA, ui.Robot4MA->text().toStdString().c_str());
    strcpy(sRobotConfigPara[3].cRobotArmGW, ui.Robot4GW->text().toStdString().c_str());
    sRobotConfigPara[3].iRobotarmType = ui.Robot4Choice->currentIndex();//当前数据currentData() 当前索引currentIndex()
    sRobotConfigPara[3].iRobotToolType = ui.Robot4EndToolChoice->currentIndex();
    strcpy(sRobotConfigPara[3].cCoordinateSystem, ui.Robot4Axis->text().toStdString().c_str());
    sRobotConfigPara[3].iMotornumber[0] = ui.Robot4Motor1->value();
    sRobotConfigPara[3].iMotornumber[1] = ui.Robot4Motor2->value();
    sRobotConfigPara[3].iMotornumber[2] = ui.Robot4Motor3->value();
    sRobotConfigPara[3].iMotornumber[3] = ui.Robot4Motor4->value();
    sRobotConfigPara[3].iMotornumber[4] = ui.Robot4Motor5->value();
    
    bool res = SRCommunicat->SaveRobotConfig();
    if(res) emit show_message(LOG_MES::logMessages["Info1"]);
    else emit show_message(LOG_MES::logMessages["Info2"]);

    return;
}
